package cc.fine.svc.main.controller.tbl;


import cc.fine.svc.common.utils.R;
import cc.fine.svc.main.bean.FineMessage;
import cc.fine.svc.main.bean.dto.VideoInfoDto;
import cc.fine.svc.main.bean.po.TblCollect;
import cc.fine.svc.main.bean.vo.CollectVo;
import cc.fine.svc.main.service.tbl.TblCollectService;
import cc.fine.svc.main.utils.MqUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyihe
 * @since 2023-10-25
 */
@RestController
@RequestMapping("/collect")
@Api(description = "用户的收藏夹管理")
public class TblCollectController {

    @Autowired
    private TblCollectService collectService;

    @Autowired
    private MqUtils mqUtils;

    @ApiOperation("获取某用户的所有收藏夹")
    @GetMapping("/{userId}")
    public R get(@PathVariable String userId) {
        QueryWrapper<TblCollect> w = new QueryWrapper<>();
        w.eq("user_id", userId);

        List<TblCollect> list = collectService.list(w);

        return R.ok().data("data", list);
    }

    @ApiOperation("用户添加收藏夹")
    @PostMapping("/")
    public R add(@RequestBody(required = false) CollectVo vo) {
        TblCollect collect = new TblCollect();
        BeanUtils.copyProperties(vo, collect);

        collectService.save(collect);

        return R.ok();
    }

    @ApiOperation("用户删除收藏夹")
    @DeleteMapping("/{collectId}")
    public R deleted(@PathVariable("collectId") String collectId) {
        collectService.removeById(collectId);
        return R.ok();
    }

    @ApiOperation("为用户收藏视频")
    @PostMapping("/{collectId}/{videoId}")
    public R collect(@PathVariable String collectId,
                     @PathVariable String videoId) {
        mqUtils.send(new FineMessage(FineMessage.Collect_Add, collectId, videoId));

        return R.ok();
    }

    @ApiOperation("为用户取消收藏视频")
    @DeleteMapping("/{collectId}/{videoId}")
    public R disCollect(@PathVariable String collectId,
                        @PathVariable String videoId) {
        mqUtils.send(new FineMessage(FineMessage.Dis_Collect_Add, collectId, videoId));

        return R.ok();
    }

    @ApiOperation("获取收藏夹视频列表")
    @GetMapping("/videos/{id}")
    public R collect(@PathVariable String id) {
        List<VideoInfoDto> list = collectService.getVideoList(id);


        return R.ok().data("list", list);
    }
}

